package servlet;

import pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author zzj
 * @Date 2020/8/5 16:28
 */
public class UserDaoImp implements UserDao {
    private DBUtil dbUtil = new DBUtil();
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public List<User> SelectAll() {
        // 模拟数据
        List<User> users = new ArrayList<>();
        Connection conn = dbUtil.init();
        String sql = "select * from tb_user";
        try {
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                users.add(new User(rs.getInt(1), rs.getString(2), rs.getInt(3)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            dbUtil.close();
        }
        return users;
    }
}
